#ifndef __SYSTEM_H
#define __SYSTEM_H
#include "gpio.h"
#include "uart.h"
#include "time.h"
#include "adc.h"
#include "math.h"
#include "BC26.h"
#include "TelecomPlatform.h"
#include	"Network_communication.h"

void SystemClock_Config(void);

#define	ERR_FAIL		  1
#define	ERR_SUCCESS		  0	
typedef struct
{
	uint16 	aVal;
	uint16	bVal;
	uint16 	cVal;
	uint16 	rVal;
} CollectCurData;

typedef struct
{
	uint16 	aVal;
	uint16	bVal;
	uint16 	cVal;
} CollectVolData;

typedef struct
{
	int16 	aVal;
	int16	bVal;
	int16 	cVal;
	int16 	sVal;

} CollectTemData;

#define		DEVEUI_Len		16
#define IMEI_Len 	15
#define	ICCID_Len	20


typedef struct
{
	CollectCurData	CurData;
	CollectVolData	VolData;
	CollectTemData	TemData;
	uint8			SignalStrength;
	uint8			OnLineFlag;
	uint8			GetDeviceID;
	uint8			LORA_DEVEUI[DEVEUI_Len+1];
	uint8 			BC26_IMEI[IMEI_Len+1];
	uint8 			BC26_ICCID[ICCID_Len+1];	
} CollectedData;

typedef enum 
{
	CurrentValue=0x01,
	VoltageValue=0x02,
	TemperatureValue=0x03,
	IMEIValue=0x04,
	DEVEUI=0x05,
	ErrorType=0x06,
	ThresholdValue=0x07,
	ChannleEnableValue=0x08
}CollectedDataType;

typedef struct
{
  uint8		run_led;
  uint8		alarm_led;
  uint8		malfunction_led;
  uint8		missingPhase_led;
  
} AllLedStatus;
extern	AllLedStatus		LedStatus;
#define  LED_ON			2
#define  LED_OFF		3


extern	CollectedData	collectData;

#define FW_VERSION  "SwV1.1-20200528"
void SendCollectedValToDisplayBoard(CollectedDataType datatype,  CollectedData *result);
void ProcessLcdUartData(void);


#define IMEI_Len 	15

#define	ICCID_Len	20

extern	int8 	SocketId;


#define	Uart_ReceBufLen		300
typedef struct
{
	uint16			len;
	uint8			buf[Uart_ReceBufLen];
	uint8			processed;
}ReceDataInfor;
extern	ReceDataInfor*gNB_ReceData;



extern	ReceDataInfor*gLCD_ReceData;

extern	uint32_t SysCnt;


typedef struct
{
	
	uint16 	TemThreshold_a;		
	uint16 	TemThreshold_b;
	uint16 	TemThreshold_c;
	uint16 	TemThreshold_s;		
	
	uint16 	Vol_OverThreshold_a; 
	uint16 	Vol_BelowThreshold_a;
	uint16 	Vol_OverThreshold_b; 
	uint16 	Vol_BelowThreshold_b;
	uint16 	Vol_OverThreshold_c; 
	uint16 	Vol_BelowThreshold_c;

	uint16 	CurThreshold_a;		
	uint16 	CurThreshold_b;
	uint16 	CurThreshold_c;
	uint16 	CurThreshold_r;		
	uint16 	PowerThreshold;	
} ThresholdVal;


typedef struct
{
  uint8		CurChannle_a;
  uint8		CurChannle_b;
  uint8		CurChannle_c;
  uint8		CurChannle_r;

  uint8		VolChannle_a;
  uint8		VolChannle_b;
  uint8		VolChannle_c;
  
  uint8		TemChannle_a;
  uint8		TemChannle_b;
  uint8		TemChannle_c;
  uint8		TemChannle_s;

} ChannleEnableStatus;


struct NetworkDecice_operations
{
	void 	(*Init)(void);
	uint8 	(*JoinNet)(void);
	uint8 	(*SendDada)(CollectedData *data);
	
};
extern	struct NetworkDecice_operations NetworkDecice;
typedef enum 
{
	  	NotDetectModule=0,
    	LoraModule = 1,
    	NBModule=2,
}BoardModuleType;



#define SuccessFull                         1
#define FAIL                            	0




#define	Not_UsedDefaultValue	0xAA	

typedef struct
{
	uint8			thresholdVal_UsedDefault;
	ThresholdVal	thresholdVal;
	uint8			ChannleStatus_UsedDefault;
	ChannleEnableStatus	ChannleStatus;
	uint8			sendDataToServerFlag;
	uint8			sendDataTimes;
	BoardModuleType	ModuleType;
	uint8			sendThresholdValueToLcd;
	uint8			sendChannleEnableValueToLcd;

	
} Sys_Para;
extern	Sys_Para			SysPara;

#define	  Current_Shift_Val		4
#define	  Voltage_Shift_Val		2
#define	  Temperature_Shift_Val	0


#define	  Cur_Warn_Normal		0
#define	  Cur_Warn_OverCurrent	1
#define	  Cur_Warn_NoSensor	    2

#define	  Vol_Warn_Normal		0
#define	  Vol_Warn_OverVoltage	1
#define	  Vol_Warn_UnderVoltage	2
#define	  Vol_Warn_CutOFF	    3

#define	  Tem_Warn_Normal	    0
#define	  Tem_Warn_OverHigh		1
#define	  Tem_Warn_NoSensor	    2

#define		Current_Zero_Point_r	100
#define		Current_Zero_Point_abc	5

#define		Voltage_Zero_Point		50



#define  Channle_Status_Close  0x55
#define  Channle_Status_Open   0xaa
extern	uint8	SendMessageToServer(CollectedData*value);
extern	void NB_Delay(__IO uint32_t Delay);

#define		Cmd1Header           	 "SetThredValue"
#define		Cmd1SuccessAck           "SetThredValueSuccess"
#define		Cmd2SendDataToServer     "SendDataToServer"
#define		Cmd2SuccessAck           "IWillSendDataToServer"

#define		Cmd3Header           	 "SetChannleEnableVal"
#define		Cmd3SuccessAck           "SetChannleEnableValSuccess"

#define		Cmd4Header           	 "SetThresholdValue"
#define		Cmd4SuccessAck           "SetThresholdValueSuccess"

#define		Cmd5Header           	 "SetChannleEnable"
#define		Cmd5SuccessAck           "SetChannleEnableSuccess"


extern	void FeedWatchDog(void);
void SysParaSave(uint32 saveaddress,uint8 *buff,uint8 len);
#define InterFlashAddress	(0x8000000+1024*63)

BoardModuleType	CheckBoardModuleType(void);
#define	VREFINT_CAL    *(__IO uint16_t *)(0X1FFFF7BA)



#endif
